import 'package:flutter/material.dart';
import 'package:sleek_circular_slider/sleek_circular_slider.dart';

class CircularSlider extends StatelessWidget {
  final void Function(double)? onChange;
  final double initialValue;
  final double min;
  final double max;
  final double? size;

  ///+号偏移
  final Offset? increaseOffset;

  ///-号偏移
  final Offset? decreaseOffset;

  ///+-背景色
  final Color? creaseBackgroundColor;
  final Widget Function(double)? innerWidget;

  ///点击增加
  final void Function()? onIncrease;

  ///点击减少
  final void Function()? onDecrease;

  ///为false时不可拖动，默认true
  final bool enable;
  const CircularSlider({
    Key? key,
    this.onChange,
    this.initialValue = 50,
    this.min = 0,
    this.max = 100,
    this.size,
    this.innerWidget,
    this.enable = true,
    this.onIncrease,
    this.onDecrease,
    this.increaseOffset,
    this.decreaseOffset,
    this.creaseBackgroundColor = Colors.black,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    print('initialValue->$initialValue');
    Widget slider = SleekCircularSlider(
        minimumTouchWidth: 35,
        appearance: CircularSliderAppearance(
          size: size ?? 150,
          customWidths: CustomSliderWidths(
              progressBarWidth: 2, trackWidth: 2, handlerSize: 8),
          customColors: CustomSliderColors(
              shadowColor: Colors.black,
              dotColor: Theme.of(context).primaryColor,
              progressBarColor: Theme.of(context).primaryColor,
              trackColor: Colors.white),
        ),
        min: min,
        max: max,
        initialValue: initialValue,
        innerWidget: innerWidget,
        onChange: onChange);
    Widget decrease = Container(
      color: creaseBackgroundColor,
      height: 35,
      width: 35,
      alignment: Alignment.topLeft,
      child: Text(
        '-',
        style: TextStyle(color: Colors.white, fontSize: 25),
      ),
    );
    if (decreaseOffset != null) {
      decrease = Transform.translate(
        offset: decreaseOffset!,
        child: decrease,
      );
    }
    Widget increase = Container(
      alignment: Alignment.topRight,
      height: 35,
      width: 35,
      color: creaseBackgroundColor,
      margin: EdgeInsets.only(top: 0),
      child: Text(
        '+',
        style: TextStyle(color: Colors.white, fontSize: 25),
      ),
    );
    if (increaseOffset != null) {
      increase = Transform.translate(offset: increaseOffset!, child: increase);
    }
    slider = Stack(
      children: [
        IgnorePointer(
          child: slider,
          ignoring: !enable,
        ),
        Align(
          alignment: Alignment.bottomLeft,
          child: GestureDetector(onTap: onDecrease, child: decrease),
        ),
        Align(
          alignment: Alignment.bottomRight,
          child: GestureDetector(onTap: onIncrease, child: increase),
        ),
      ],
    );
    slider = SizedBox(
      child: slider,
      width: size,
      height: size,
    );
    return slider;
  }
}
